package com.example.qxfw.dataBaseManager.nyzjk.controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.ny.AgriServiceIndex;
import com.example.qxfw.dataBaseManager.nyzjk.service.NshdzbkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/************************************
 *
 * @author : HuangDeYin Jolly
 * @since : JDK 11
 * @Date : 2023/7/6  星期四   0:55
 * @description : 此类用来
 ***************************************/
@Slf4j
@Api(tags = "数据库管理-农业专家库-农事活动指标库")
@RestController
@CrossOrigin
@RequestMapping("/NshdzbkController")
public class NshdzbkController {

    @Autowired
    private NshdzbkService nshdzbkService;

    @ApiOperation("去重查询")
    @GetMapping("selectDistinct")
    public SysResult selectDistinct(String column) {
        List<AgriServiceIndex> agriServiceIndices = this.nshdzbkService.selectDistinct(column);
        return SysResult.success(agriServiceIndices);
    }

    @ApiOperation("查询所有农事服务指标")
    @GetMapping("selectAll")
    public SysResult selectAll() {
        List<AgriServiceIndex> agriServiceIndexList = this.nshdzbkService.selectAll();
        return SysResult.success(agriServiceIndexList);
    }

    @ApiOperation("查询")
    @GetMapping("querySelect")
    public SysResult querySelect(@RequestBody AgriServiceIndex agriServiceIndex) {
        List<AgriServiceIndex> agriServiceIndexList = this.nshdzbkService.queryList(agriServiceIndex);
        return SysResult.success(agriServiceIndexList);
    }

    @ApiOperation("修改农事服务指标")
    @PostMapping("updateAgriServiceIndex")
    public SysResult updateAgriServiceIndex(@RequestBody AgriServiceIndex agriServiceIndex) {
        Integer updateAgriServiceIndex = this.nshdzbkService.updateAgriServiceIndex(agriServiceIndex);
        if (updateAgriServiceIndex > 0) {
            return SysResult.success();
        } else {
            return SysResult.fail();
        }
    }

    @ApiOperation("删除农事服务指标")
    @PostMapping("deleteAgriServiceIndex")
    public SysResult deleteAgriServiceIndex(String id) {
        Integer deleteAgriServiceIndex = this.nshdzbkService.deleteAgriServiceIndex(id);
        if (deleteAgriServiceIndex > 0) {
            return SysResult.success();
        } else {
            return SysResult.fail();
        }
    }

    @ApiOperation("添加农事服务指标")
    @PostMapping("saveAgriServiceIndex")
    public SysResult saveAgriServiceIndex(@RequestBody AgriServiceIndex agriServiceIndex) {
        List<AgriServiceIndex> agriServiceIndexList = this.nshdzbkService.querySelect5(agriServiceIndex);
        if (agriServiceIndexList.size()>0){
            return SysResult.fail("存在相同指标");
        }
        String maxId = this.nshdzbkService.selectMaxId();
        agriServiceIndex.setId(String.valueOf(Integer.parseInt(maxId) + 1));
        Integer saveAgriServiceIndex = this.nshdzbkService.saveAgriServiceIndex(agriServiceIndex);
        if (saveAgriServiceIndex > 0) {
            return SysResult.success();
        } else {
            return SysResult.fail();
        }
    }


}
